﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Caching
{
    /// <summary>
    /// 缓存扩展类
    /// </summary>
    public static class CacheExtensions
    {
        /// <summary>
        /// 获取缓存实体，如果当前未缓存，那么缓存该实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheManager"></param>
        /// <param name="key"></param>
        /// <param name="acquire"></param>
        /// <returns></returns>
        public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire) where T : BaseEntity
        {
            if (cacheManager.Set_Contains(key))
            {
                cacheManager.Item_Get<T>(key);
                return default(T);
            }
            else
            {
                var result = acquire();
                cacheManager.Item_Set<T>(key, result);
                return result;
            }

        }
    }
}
